package IO练习.Io缓冲流;

import java.io.*;

public class TestBufferedStream {
   /*
      1.缓冲流
      BufferedInputStream
      BufferedOutputStream
      BufferedReader
      BufferedWriter

      2.作用：提供流的读取，写入的速度
   */
   public static void main(String[] args) {
      function();
   }

   public static void function() {
      BufferedInputStream bis = null;
      BufferedOutputStream bos = null;
      FileInputStream fis = null;
      FileOutputStream fos = null;
      try {
         File file1 = new File("A.txt");
         File file2 = new File("B.txt");
         fis = null;
         fos = null;
         //2.1制造节点流
         fis = new FileInputStream(file1);
         fos = new FileOutputStream(file2,true);//在OutputStream使用两个参的构造器，如果第二个参数是true,那么就可以在原来的基础之上进行不覆盖复制
         //2.2制造缓冲流
         bis = new BufferedInputStream(fis);
         bos = new BufferedOutputStream(fos);

         //3.制造承装元素的数组
         byte[] bytes = new byte[5];
         int len;
         while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
         }
      } catch (IOException e) {
         throw new RuntimeException(e);
      } finally {
         if (bis != null && fis != null) {
            try {
               bis.close();
               fis.close();
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
         }

         if (bos != null && fos != null) {
            try {
               bos.close();
               fos.close();
            } catch (IOException e) {
               throw new RuntimeException(e);
            }
         }
      }
   }
}
